/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.portlet.editor.gui.view.internal.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.genia.toolbox.portlet.editor.gui.PortletEditorGUI;
import com.genia.toolbox.portlet.editor.gui.view.internal.PortletDocumentInternalFrame;
import com.genia.toolbox.portlet.editor.model.portlet.PortletType;
import com.genia.toolbox.portlet.editor.model.portlet.impl.DispatcherPortletModel;
import com.genia.toolbox.portlet.editor.model.portlet.impl.PortletModel;

/**
 * Dispatcher portlet view panel.
 */
@SuppressWarnings("serial")
public class DispatchedPortletViewPanel
    extends AbstractPortletViewPanel
{

  /**
   * The ID label.
   */
  private JLabel labelId = null;

  /**
   * The URL label.
   */
  private JLabel labelURL = null;



  /**
   * Constructor.
   * 
   * @param portletEditorGUI
   *          the portletEditorGUI.
   * @param internalFrame
   *          the internal frame.
   */
  public DispatchedPortletViewPanel(PortletEditorGUI portletEditorGUI, PortletDocumentInternalFrame internalFrame)
  {
    super(portletEditorGUI, internalFrame);
  }



  /**
   * Create the portlet type.
   * 
   * @return the portlet type.
   */
  @Override
  public PortletType getElementType()
  {
    return PortletType.Dispatcher;
  }



  /**
   * Create the specific panel.
   * 
   * @return the specific panel.
   */
  @Override
  public JPanel createSpecificPanel()
  {
    JPanel panel = new JPanel();

    GridBagLayout layout = new GridBagLayout();
    GridBagConstraints constraints = new GridBagConstraints();
    panel.setLayout(layout);

    panel.addMouseListener(new MouseAdapter()
    {
      /**
       * Handle the mouse clicked event.
       * 
       * @param e
       *          the mouse event.
       */
      @Override
      public void mouseClicked(MouseEvent e)
      {
        DispatchedPortletViewPanel.this.deactivateParent();
        if (DispatchedPortletViewPanel.this.isCommonSelected()) {
          DispatchedPortletViewPanel.this.deactivateAll();
          DispatchedPortletViewPanel.this.getGui().displayCurrentDocumentSettings();
        }
        else {
          DispatchedPortletViewPanel.this.activateCommon(true);
          DispatchedPortletViewPanel.this.deactivateSpecific();
          DispatchedPortletViewPanel.this.getGui().displayElementSettings(DispatchedPortletViewPanel.this.getElement());
        }
      }



      /**
       * Handle the mouse pressed event.
       * 
       * @param e
       *          the mouse event.
       */
      @Override
      public void mousePressed(MouseEvent e)
      {
        DispatchedPortletViewPanel.this.maybeShowPopup(e);
      }



      /**
       * Handle the mouse released event.
       * 
       * @param e
       *          the mouse event.
       */
      @Override
      public void mouseReleased(MouseEvent e)
      {
        DispatchedPortletViewPanel.this.maybeShowPopup(e);
      }
    });

    this.labelId = new JLabel();
    this.labelId.setHorizontalAlignment(SwingConstants.CENTER);
    this.labelId.setVerticalAlignment(SwingConstants.TOP);
    this.labelId.setVerticalTextPosition(SwingConstants.TOP);
    constraints.gridx = 1;
    constraints.gridy = 1;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.insets = new Insets(0, 0, 0, 0);
    panel.add(this.labelId, constraints);

    this.labelURL = new JLabel();
    this.labelURL.setHorizontalAlignment(SwingConstants.CENTER);
    this.labelURL.setVerticalAlignment(SwingConstants.TOP);
    this.labelURL.setVerticalTextPosition(SwingConstants.TOP);
    constraints.gridx = 1;
    constraints.gridy = 2;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.weightx = 1;
    constraints.weighty = 0;
    constraints.fill = GridBagConstraints.HORIZONTAL;
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.insets = new Insets(5, 0, 0, 0);
    panel.add(this.labelURL, constraints);

    return panel;
  }



  /**
   * Set the displayed portlet.
   * 
   * @param model
   *          the displayed portlet model.
   */
  @Override
  public void setElement(PortletModel model)
  {
    super.setElement(model);
    DispatcherPortletModel dispatcherModel = (DispatcherPortletModel) model;
    this.getLabelId().setText(" ");
    this.labelId.setText(dispatcherModel.getId());
    this.labelURL.setText(dispatcherModel.getUrl());
  }



  /**
   * Handle the common panel activation.
   * 
   * @param isActivated
   *          Whether the panel is activated.
   */
  @Override
  public void activateCommon(boolean isActivated)
  {
    super.activateCommon(isActivated);
    this.activate(isActivated, this.getSpecificPanel());
  }

}
